package com.ruoyi.common.modbus.data;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.util.encoders.Hex;

/**
 * 类功能说明：心跳指令数据<br/>
 * 公司名称：未可自动化 <br/>
 * 作者：weikeauto <br/>
 */
public class MiiHeartBeatData extends MiiStringData {
	
	static final int DEVICE_GROUPNO_INDEX = 1;

	private String shebei;

	public MiiHeartBeatData(byte[] datas) {
		super(datas);
		shebei = hexStringToString(Hex.toHexString(datas));
	}

	public static String hexStringToString(String hex) {
		byte[] bytes = new byte[hex.length() / 2];
		for (int i = 0; i < hex.length(); i += 2) {
			bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i+1), 16));
		}
		return new String(bytes);
	}

	
	/**
	 * 返回设备组编码
	 * @return 设备编码
	 */
	public String deviceGroup() {
		return StringUtils.isBlank(content()) ? 
				String.valueOf(toBytes()[DEVICE_GROUPNO_INDEX]) : content();
	}

	public String shebei(){
		return shebei;
	}


}
